using System;
using System.Collections.Generic;
using System.Text;
using Barrage;
using Barrage.Meshes.Text;
using Spacedash.Logic;
using SlimDX;
using Barrage.Language;

namespace Spacedash.Game.Items
{
    class GUIShipOffsets : Item
    {
        const string POSITIONS = "POSITIONS";
        List<TextMesh> textMeshes=new List<TextMesh>(5);
        Ship ship;
        Race race;
        public GUIShipOffsets(Vector2 position, Race race, Ship ship)
        {
            this.ship = ship;
            this.race = race;
            motion.Translation = new Vector3(position, 0);
            TextDrawProperties tdp = new TextDrawProperties(0.25f, Vector2.Zero);

            BitmapFont bf = BitmapFontManager.Fonts["Standard"];
            TextMesh tm =  new TextMesh(bf,tdp);
            textMeshes.Add(tm);
            addMesh(tm);
            tm.Text = LanguageManager.Texts[POSITIONS];
            for(int i=0;i<race.Ships.Count;i++)
            {
                tm = new TextMesh(bf,tdp);
                tm.Offset = new Vector2(0, -bf.FontInfo.LineHeight * 0.25f * (i+1));
                textMeshes.Add(tm);
                addMesh(tm);
            }            
        }

        protected override void DoUpdate()
        {
            for(int i=0;i<race.OrderedShips.Values.Count;i++)
            {
                Ship s =race.OrderedShips.Values[i];
                if (s != ship)
                {
                    float offset = ship.OffsetToShip(s);
                    textMeshes[i + 1].Text = String.Format("{0}-{1,8} {2}{3:0.0}\n", s.RacePosition + 1, s.Profile.Name.Substring(0, Math.Min(8, s.Profile.Name.Length)), offset>0?"+":"", offset);
                }
                else
                    textMeshes[i + 1].Text = String.Format("{0}-{1,8}\n", s.RacePosition + 1, s.Profile.Name.Substring(0, Math.Min(8, s.Profile.Name.Length)));
                textMeshes[i+1].FirstMaterial.Diffuse = (Color4)s.Color;
            }
            base.DoUpdate();
        }
    }
}
